package luj.tool.jnproxyan.proxy.util.data.load;

import luj.data.api.Data;
import luj.tool.jnproxyan.proxy.database.DatabaseState;
import luj.tool.jnproxyan.proxy.util.data.load.internal.CacheKeyMaker;
import luj.tool.jnproxyan.proxy.util.data.load.internal.DataDeepLoader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class DataByIdLoader {

  public DataByIdLoader inject(Class<?> dataType, Long dataId, DatabaseState dbState, Map<String, Data> loadCache) {
    DataByIdLoader result = new DataByIdLoader();
    result._dataType = dataType;
    result._dataId = dataId;
    result._dbState = dbState;
    result._loadCache = loadCache;

    result._deepLoader = _deepLoader;
    result._keyMaker = _keyMaker;

    return result;
  }

  public Data load() {
    _deepLoader.inject(_dataType, _dataId, _dbState, _loadCache).load();

    String key = _keyMaker.make(_dataType, _dataId);
    return _loadCache.get(key);
  }

  Class<?> _dataType;
  Long _dataId;

  DatabaseState _dbState;
  Map<String, Data> _loadCache;

  @Autowired
  DataDeepLoader _deepLoader;

  @Autowired
  CacheKeyMaker _keyMaker;
}
